﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using AgileObjects.AgileMapper;
using AgileObjects.AgileMapper.Extensions;
using BDSync.Domains.DBConn;
using BDSync.Domains.Entities;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using MSSQL.DBSync.UI.Common.Messages;
using MSSQL.DBSync.UI.Components.VOS;

namespace MSSQL.DBSync.UI.Components.ManageConn
{
    public class ManageConnVM : ViewModelBase, IManageConnVM
    {

        private IDBConnService mSvc;

        public ManageConnVM()
        {
            initDesignData();
        }

        public ManageConnVM(IDBConnService svc)
        {
            mSvc = svc;
            initDesignData();
        }

        private void initDesignData()
        {
            Conns = new ObservableCollection<ConnObjVO>();
            //Conns.Add(new ConnObjVO { Host = "10.9.0.6", DB = "eComm", PWD = "Abc@123456!!", User = "srapp" });
            //Conns.Add(new ConnObjVO { Host = "10.9.0.6", DB = "eCommTest", PWD = "Abc@123456!!", User = "srapp" });
        }

        /// <summary>
        /// 数据库连接集合
        /// </summary>
        public ObservableCollection<ConnObjVO> Conns { get; set; }

        private ConnObjVO mCurConn;
        /// <summary>
        /// 数据库
        /// </summary>
        public ConnObjVO CurConn
        {
            get
            {
                return mCurConn;
            }
            set
            {
                Set(ref mCurConn, value);
            }
        }


        private RelayCommand mAddConn;
        /// <summary>
        /// 添加
        /// </summary>
        public RelayCommand AddConn
        {
            get
            {
                //非空判断
                if (mAddConn != null) { return mAddConn; }
                mAddConn = new RelayCommand(() =>
                {
                    Conns.Add(new ConnObjVO());
                });
                return mAddConn;
            }
            set { mAddConn = value; }
        }


        private RelayCommand mDeleteConn;
        /// <summary>
        /// 删除
        /// </summary>
        public RelayCommand DeleteConn
        {
            get
            {
                //非空判断
                if (mDeleteConn != null) { return mDeleteConn; }

                mDeleteConn = new RelayCommand(() =>
                {
                    if (CurConn == null)
                    {
                        return;
                    }
                    var ent = Mapper.Map(CurConn).ToANew<DBConnObj>();
                    mSvc.Delete(ent);
                    GetAllHandler();

                    Messenger.Default.Send("", MessageTopics.APP_Conn_Update);
                    Messenger.Default.Send("删除成功", MessageTopics.APP_ShowMessage);
                });

                //Messenger.Default.Send("删除成功", MessageTopics.APP_ShowMessage);

                return mDeleteConn;
            }
            set { mDeleteConn = value; }
        }


        private RelayCommand mSaveConn;
        /// <summary>
        /// 修改
        /// </summary>
        public RelayCommand SaveConn
        {
            get
            {
                //非空判断mSaveConn
                if (mSaveConn != null) { return mSaveConn; }
                mSaveConn = new RelayCommand(() =>
                {
                    foreach (var x in Conns)
                    {
                        var res = x.SelfCheck();
                        if (!string.IsNullOrWhiteSpace(res))
                        {
                            Messenger.Default.Send(res, MessageTopics.APP_ShowError);
                            return;
                        }
                    }

                    var ents = Mapper.Map(Conns).ToANew<List<DBConnObj>>();
                    mSvc.Update(ents);
                    GetAllHandler();

                    Messenger.Default.Send("", MessageTopics.APP_Conn_Update);
                    Messenger.Default.Send("保存成功",MessageTopics.APP_ShowMessage);
                });
                return mSaveConn;
            }
            set { mSaveConn = value; }
        }


        private RelayCommand mGoBack;
        /// <summary>
        /// 返回
        /// </summary>
        public RelayCommand GoBack
        {
            get
            {
                //非空判断mSaveConn
                if (mGoBack != null) { return mGoBack; }
                mGoBack = new RelayCommand(() =>
                {
                    Messenger.Default.Send("", MessageTopics.APP_GoBack);
                });
                return mGoBack;
            }
            set { mGoBack = value; }
        }


        private RelayCommand mGetALL;
        /// <summary>
        /// 获取所有
        /// </summary>
        public RelayCommand GetALL
        {
            get
            {
                //非空判断mSaveConn
                if (mGetALL != null) { return mGetALL; }
                mGetALL = new RelayCommand(() => { GetAllHandler(); });
                return mGetALL;
            }
            set { mGetALL = value; }
        }

        private void GetAllHandler()
        {
            var res = mSvc.FindAll();
            var objs = Mapper.Map(res).ToANew<List<ConnObjVO>>();
            Conns.Clear();
            objs.ForEach(x => Conns.Add(x));
        }
    }
}
